package edu.kpi.pzks2.ui.system;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

import edu.kpi.pzks2.graph.Edge;
import edu.kpi.pzks2.graph.GraphBundle;
import edu.kpi.pzks2.graph.Node;
import edu.kpi.pzks2.ui.GraphEditor;
import edu.kpi.pzks2.ui.GraphMenuBarListeners;
import edu.kpi.pzks2.ui.GraphMenuBarListeners.LoadListener;
import edu.kpi.pzks2.ui.GraphToolBar;
import edu.kpi.pzks2.ui.MainFrame.LoadLabel;
import edu.uci.ics.jung.graph.Graph;

@SuppressWarnings("serial")
public class SystemFrame extends JFrame implements LoadListener<System> {
    public static final String TITLE = "System editor";
    
    private int chanelCount;
    private boolean duplex;
    
    final SystemEditor graphEditor;
    
    public SystemFrame(final LoadLabel loadLabel) {
        setTitle(TITLE);

        setLayout(new BorderLayout());
        
        graphEditor = new SystemEditor();
        add(new JScrollPane(graphEditor), BorderLayout.CENTER);

        setJMenuBar(buildMenu(this, graphEditor));
        add(new GraphToolBar(graphEditor, loadLabel), BorderLayout.NORTH);
        
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
    }
    
    private JMenuBar buildMenu(final Component parent, final GraphEditor graphEditor) {
        final JMenuBar menubar = new JMenuBar();
        
        final JMenu fileMenu = new JMenu("File");
        menubar.add(fileMenu);
        
        final JMenuItem newItem = new JMenuItem("New");
        final JMenuItem openItem = new JMenuItem("Open");
        final JMenuItem saveItem = new JMenuItem("Save");
        final JMenuItem saveAsItem = new JMenuItem("Save as");

        newItem.addActionListener(GraphMenuBarListeners.getNewListener(graphEditor));
        openItem.addActionListener(GraphMenuBarListeners.getOpenListener(this, parent));
        saveItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GraphMenuBarListeners.save(
                        new System(new GraphBundle(graphEditor.getGraphLayout()),
                                chanelCount, duplex), parent);
            }
        });
        
        fileMenu.add(newItem);
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.add(saveAsItem);
        
        final JMenu helpMenu = new JMenu("Help");
        menubar.add(helpMenu);
        
        final JMenuItem helpItem = new JMenuItem("Help");
        helpMenu.add(helpItem);
        
        return menubar;
    }

    public void objectLoaded(System loadedObject) {
        graphEditor.setGraphBundle(loadedObject.getSystem());
    }

    public int getChanelCount() {
        return chanelCount;
    }
    
    public boolean isDuplex() {
        return duplex;
    }
    
    public Graph<Node, Edge> getSystemGraph() {
        return graphEditor.getSystemGraph();
    }
}
